package com.nervous.jiaoli.face.tag;

import com.nervous.jiaoli.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/face/tag")
public class TagAPI {
    @Resource
    private TagService tagService;

    @GetMapping("/preview")
    public Result tagList(TagDto tagDto){
        List<Tag> tagList = tagService.getTagList(tagDto);
        return Result.success("查询tag成功！",tagList);
    }

    @PostMapping("/create")
    public Result createTag(@RequestBody TagDto tagDto){
        boolean tagExists = tagService.checkIfExists(tagDto);
        if (!tagExists){
            tagService.createTag(tagDto);
            return Result.success("创建tag成功！");
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"tag已存在！");
    }

    @PostMapping("/drop")
    public Result dropTag(@RequestBody TagDto tagDto){
        boolean tagExists = tagService.checkIfExists(tagDto);
        if (tagExists){
            int number = tagService.deleteNumber(tagDto);
            List<Tag> tagList = tagService.getTagList(tagDto);
            tagService.dropTag(tagDto);
            return Result.success("删除tag成功，共删除"+number+"个tag！",tagList);
        }
        return Result.fail(Result.ERR_CODE_BUSINESS,"tag不存在！");
    }
}
